import axios from 'axios'
import NProress from 'nprogress' // 展示进度条的js库
import 'nprogress/nprogress.css'
import { getUserTempId, readToken } from '@/utils/auth'


const myAxios = axios.create({
  baseURL: 'http://sph-h5-api.atguigu.cn',
  timeout: 5000,
})

// 请求拦截器
myAxios.interceptors.request.use((config) => {
  NProress.start() // 启用进度条
  //向请求头中添加userTempId
  config.headers.userTempId = getUserTempId()
  //token
  const token = readToken()
  token && (config.headers.token = token)
  return config
})

//  响应拦截器
myAxios.interceptors.response.use(
  (response) => {
    NProress.done()
    // 判断token是否过期
    if (response.data.code === 208) {
    
      return new Promise(() => {})
    }
    return response.data
  },
  (error) => {
    NProress.done()
    return Promise.reject(error)
  }
)

export default myAxios